<?php

/**
 * ECSHOP 管理中心优惠活动管理
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: favourable.php 17217 2011-01-19 06:29:08Z liubo $
 */
 
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');

/*------------------------------------------------------ */
//-- 活动列表页
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'list')
{
    admin_priv('users_manage');


    /* 模板赋值 */
    $smarty->assign('full_page',   1);
    $smarty->assign('ur_here',     '提现账户');
    $smarty->assign('action_link', array('href' => 'mshop_cashbank.php?act=add', 'text' => '添加提现账户'));

    $list = mshop_cashbank_list();
// print_r($list);exit;

    $smarty->assign('mshop_cashbank_list', $list['item']);
    $smarty->assign('filter',          $list['filter']);
    $smarty->assign('record_count',    $list['record_count']);
    $smarty->assign('page_count',      $list['page_count']);

    $sort_flag  = sort_flag($list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    /* 显示商品列表页面 */
    assign_query_info();
    $smarty->display('mshop_cashbank_list.htm');
}

/*------------------------------------------------------ */
//-- 分页、排序、查询
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'query')
{
    $list = mshop_cashbank_list();

    $smarty->assign('mshop_cashbank_list', $list['item']);
    $smarty->assign('filter',          $list['filter']);
    $smarty->assign('record_count',    $list['record_count']);
    $smarty->assign('page_count',      $list['page_count']);

    $sort_flag  = sort_flag($list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    make_json_result($smarty->fetch('mshop_cashbank_list.htm'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));
}

/*------------------------------------------------------ */
//-- 删除
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
    check_authz_json('users_manage');

    $id = intval($_GET['id']);
    $mshop_cashbank = mshop_cashbank_info($id);


    if (empty($mshop_cashbank))
    {
        make_json_error($_LANG['favourable_not_exist']);
    }
    $name = $mshop_cashbank['cash_name'];


       
    $sql=" delete from ".$GLOBALS['ecs']->table('mshop_cashbank')." where cash_id=".$id;
  
    $status=$GLOBALS['db']->query($sql);
   if($status)
   {

     $sql = "update " . $ecs->table('mshop') .
            " set cash_id='' WHERE mshop_id=".$mshop_cashbank['mshop_id'];
     $status2=$db->query($sql);    
   }



    /* 记日志 */
    admin_log($name, 'remove', 'mshop_cashbank');

    /* 清除缓存 */
    clear_cache_files();

    $url = 'mshop_cashbank.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
    ecs_header("Location: $url\n");
    exit;
}

/*------------------------------------------------------ */
//-- 批量操作
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'batch')
{

    /* 取得要操作的记录编号 */
    if (empty($_POST['checkboxes']))
    {
        sys_msg($_LANG['no_record_selected']);
    }
    else
    {
        /* 检查权限 */
        admin_priv('users_manage');


        $ids = $_POST['checkboxes'];


        if (isset($_POST['drop']))
        {
       
            //对应的店铺
            foreach ($ids as $k_id => $v_id) {
                     $v_cashbank = mshop_cashbank_info($v_id);
                     $mshop_list[]=$v_cashbank['mshop_id'];

                }

           /* 删除记录 */
            $sql = "DELETE FROM " . $ecs->table('mshop_cashbank') .
                    " WHERE cash_id " . db_create_in($ids);
            $status=$db->query($sql); 

            if($status)
            {
                 $sql = "update " . $ecs->table('mshop') .
                    " set cash_id='' WHERE mshop_id ". db_create_in($mshop_list);
                 $status2=$db->query($sql);   
            }

            /* 记日志 */
            admin_log('', 'batch_remove', 'mshop_cashbank');

            /* 清除缓存 */
            clear_cache_files();

            $links[] = array('text' => '返回店铺列表', 'href' => 'mshop_cashbank.php?act=list&' . list_link_postfix());
            sys_msg($_LANG['batch_drop_ok']);
        }
    }
}


/*------------------------------------------------------ */
//-- 添加、编辑
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'add' || $_REQUEST['act'] == 'edit')
{
    /* 检查权限 */
    admin_priv('users_manage');

    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'add';
    $smarty->assign('form_action', $is_add ? 'insert' : 'update');


    /* 初始化、取得优惠活动信息 */
    if ($is_add)
    {

        $mshop_cashbank = array(
            'cash_id'        => '',
            'pay_id'      => '',
            'cash_name'      => '',
            'cash_value'    => '',
        );
        if($_GET['mshop_id'])
        $smarty->assign('mshop_id', $_GET['mshop_id']);
        else
        {
            sys_msg($_LANG['relate_mshop']);exit();
        }
    }
    else
    {
        if (empty($_GET['id']))
        {
            sys_msg('invalid param');
        }
        $id = intval($_GET['id']);
        $mshop_cashbank = mshop_cashbank_info($id);
        if (empty($mshop_cashbank))
        {
            sys_msg($_LANG['favourable_not_exist']);
        }

    }
    $smarty->assign('mshop_cashbank', $mshop_cashbank);


   //提现方式
    $sql=" select pay_id,pay_name from ".$ecs->table('mshop_payment')." where enabled=1 ";
    // ." where enabled=1";
    $payment_list=$db->getAll($sql);
    if(!$mshop_cashbank['pay_id'])//默认提现方式
        $mshop_cashbank['pay_id']=$payment_list[0]['pay_id'];
    $smarty->assign('payment_list', $payment_list);


    /* 显示模板 */
    if ($is_add)
    {

        $smarty->assign('ur_here', $_LANG['add_favourable']);
    }
    else
    {
        $smarty->assign('ur_here', $_LANG['edit_favourable']);
    }
    $href = 'mshop_cashbank.php?act=list';
    if (!$is_add)
    {
        $href .= '&' . list_link_postfix();
    }
    $smarty->assign('action_link', array('href' => $href, 'text' => $_LANG['favourable_list']));
    assign_query_info();
    $smarty->display('mshop_cashbank_info.htm');
}

/*------------------------------------------------------ */
//-- 添加、编辑后提交
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'insert' || $_REQUEST['act'] == 'update')
{
    /* 检查权限 */
    admin_priv('users_manage');

    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'insert';

    /* 检查可提现积分 */
    $pay_id = $_POST['pay_id']? $_POST['pay_id']:"";
    $cash_name = $_POST['cash_name']? $_POST['cash_name']:"";
    $cash_value = $_POST['cash_value']? $_POST['cash_value']:"";
    $mshop_id = $_POST['mshop_id']? $_POST['mshop_id']:"";

    $id = intval($_POST['id'])? intval($_POST['id']) : 0;

    

    /* 提交值 */
    $mshop_cashbank = array(
            'cash_id'        => $id,
            'pay_id'      => $pay_id,
            'cash_name'      =>$cash_name,
            'cash_value'    =>$cash_value,

    );





    /* 保存数据 */
    if ($is_add)
    {
        $mshop_cashbank['ctime']=time();

        $db->autoExecute($ecs->table('mshop_cashbank'), $mshop_cashbank, 'INSERT');
        $mshop_cashbank['mshop_cashbank_id'] = $db->insert_id();
        
        $t_mshop['cash_id']= $mshop_cashbank['mshop_cashbank_id'];
        if( $t_mshop['cash_id'])
        $db->autoExecute($ecs->table('mshop'), $t_mshop, 'UPDATE', "mshop_id = '$mshop_id'");
    }
    else
    {
        $db->autoExecute($ecs->table('mshop_cashbank'), $mshop_cashbank, 'UPDATE', "cash_id = '$id'");
         
    }

    /* 记日志 */
    if ($is_add)
    {
        admin_log('添加提现账户'.$mshop_cashbank['cash_id'], 'add', 'mshop_cashbank');
    }
    else
    {
        admin_log('编辑提现账户'.$mshop_cashbank['cash_id'], 'edit', 'mshop_cashbank');
    }

    /* 清除缓存 */
    clear_cache_files();

    /* 提示信息 */
    if ($is_add)
    {
        $links = array(
            array('href' => 'mshop_cashbank.php?act=add', 'text' => $_LANG['continue_add_favourable']),
            array('href' => 'mshop_cashbank.php?act=list', 'text' => $_LANG['back_favourable_list'])
        );
        sys_msg($_LANG['add_favourable_ok'], 0, $links);
    }
    else
    {
        $links = array(
            array('href' => 'mshop_cashbank.php?act=list&' . list_link_postfix(), 'text' => $_LANG['back_favourable_list'])
        );
        sys_msg($_LANG['edit_favourable_ok'], 0, $links);
    }
}

/*------------------------------------------------------ */
//-- 搜索商品
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'search')
{
    /* 检查权限 */
    check_authz_json('mshop_cashbank');

    include_once(ROOT_PATH . 'includes/cls_json.php');

    $json   = new JSON;
    $filter = $json->decode($_GET['JSON']);
    $filter->keyword = json_str_iconv($filter->keyword);
    if ($filter->act_range == FAR_ALL)
    {
        $arr[0] = array(
            'id'   => 0,
            'name' => $_LANG['js_languages']['all_need_not_search']
        );
    }
    else
    {
       $arr=$mshop_cashbank_list;
    }

    if (empty($arr))
    {
        $arr = array(0 => array(
            'id'   => 0,
            'name' => $_LANG['search_result_empty']
        ));
    }

    make_json_result($arr);
}

/*
 * 取得店铺列表
 * @return   array
 */
function mshop_cashbank_list()
{
    $result = get_filter();

    if ($result === false)
    {
        /* 过滤条件 */
        $filter['keyword']    = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);
        if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1)
        {
            $filter['keyword'] = json_str_iconv($filter['keyword']);
        }

        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'g.cash_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

        $where = "";
        if (!empty($filter['keyword']))
        {
            $where .= " AND g.cash_name LIKE '%" . mysql_like_quote($filter['keyword']) . "%'";
        }

        $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('mshop_cashbank') ." as g"
                ." left join ".$GLOBALS['ecs']->table('mshop_payment')."  as p on p.pay_id=g.pay_id "
                . " WHERE 1 $where";
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        

        /* 分页大小 */
        $filter = page_and_size($filter);

        // $filter['page_size']=15;//测试分页效果
        // $filter['page_count']=1;
        // print_r($__REQUEST);exit();


        /* 查询 */
        $sql = "SELECT g.*,m.mshop_id from "
              ."( SELECT c.*,p.pay_name "
                ."FROM " . $GLOBALS['ecs']->table('mshop_cashbank')." as c"
                ." left join ".$GLOBALS['ecs']->table('mshop_payment')."  as p on p.pay_id=c.pay_id "
                .") as g"
                ." left join ".$GLOBALS['ecs']->table('mshop')."  as m on m.cash_id=g.cash_id " 
                ." WHERE 1 $where "
                ." ORDER BY $filter[sort_by] $filter[sort_order] "
                ." LIMIT ".$filter['start'] .", $filter[page_size]";


        $filter['keyword'] = stripslashes($filter['keyword']);


        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->query($sql);





    $list = array();


    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        if($row['mshop_id'])
        {
          $sql="SELECT user_name,mobile_phone "
                ."FROM " . $GLOBALS['ecs']->table('users')
                ." where mshop_id=".$row['mshop_id'];
          $user=$GLOBALS['db']->getRow($sql);
          $row['user_name']=$user['user_name'];
          $row['mobile_phone']=$user['mobile_phone'];          
        }


        $row['ctime']=date("Y-m-d H:i:s",$row['ctime']);
        $list[]=$row;
    }  

    return array('item' => $list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}

   function mshop_cashbank_info($id)
   {
      $sql = "SELECT g.*,m.mshop_id from "
              ."( SELECT c.*,p.pay_name "
                ."FROM " . $GLOBALS['ecs']->table('mshop_cashbank')." as c"
                ." left join ".$GLOBALS['ecs']->table('mshop_payment')."  as p on p.pay_id=c.pay_id "
                ." where c.cash_id=".$id.") as g"
                ." left join ".$GLOBALS['ecs']->table('mshop')."  as m on m.cash_id=g.cash_id ";


        $row=$GLOBALS['db']->getRow($sql);
        $row['ctime']=date("Y-m-d H:i:s",$row['ctime']);


        $sql="SELECT user_name,mobile_phone "
                ."FROM " . $GLOBALS['ecs']->table('users')
                ." where mshop_id=".$row['mshop_id'];
        $user=$GLOBALS['db']->getRow($sql);
        $row['user_name']=$user['user_name'];
        $row['mobile_phone']=$user['mobile_phone'];
        return $row;
   }



?>